/*
 Typical use of Groovy to drive a Java API call
 
 This example is a work in progress with respect to long options:
 
 http://www.nabble.com/How-much-of-CliBuilder-does-work--td16111825.html
 
 For now use short option names.
 
 */
 
 /*
 Copyright 2009 Homosuperiorus LLC 

 Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
 this file except in compliance with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0 

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 implied. See the License for the specific language governing
 permissions and limitations under the License.

 */

import org.taglets.pub.tags.*
import org.taglets.exception.*

def cli = new CliBuilder(usage: 'groovy tagmostrecent.groovy -n [h]', parser: new org.apache.commons.cli.GnuParser ())
cli.h(longOpt: 'help', 'usage information')
cli.n(argName:'number', longOpt:'number', args:1, required: true, 'number of most recent')

def opt = cli.parse(args)
if (!opt) {
	return
}
if( opt.h ) {
	cli.usage
}

number = opt.n

try {
    client = new TagMostRecentClient()
	response = client.getMostRecent(Integer.parseInt(number))
	println "Comment post HTTP response code: ${response.getResponseCode()} ${response.getResponseMessage()}"
	println "Comment post succeeded: ${response.succeeded}"
	println "Headers:"
	def headers = response.getHeaderFields()
	headers.each { header -> println "    ${header}" }
} catch (TagletsException e) {
	e.printStackTrace();
}

